
class Planel extends egret.Sprite {
    public constructor() {
        super()
        this.init()
    }

    private stage_height: number = 800
    private stage_width: number = 800
    private plane_speed: number = 10
    private plane: egret.Bitmap
    private zdlist: egret.Bitmap[] = [];
    private init() {
        this.plane = new egret.Bitmap()
        this.plane.texture = RES.getRes('plane_1_png')
        this.plane.width = 47
        this.plane.height = 126
        // this.plane.x = 0
        // this.plane.y = 0
        this.plane.y = this.stage_height - this.plane.height
        this.plane.x = (this.stage_width - 47) / 2
        this.addChild(this.plane)
        document.addEventListener('keydown', (e) => {
            this.keydown.apply(this, [e]);
        });
        
        this.addEventListener(egret.Event.ENTER_FRAME, this.enter_frame, this)

    }

    private keydown(e) {
        console.log(e);
        const code = e.code
        if (code == 'ArrowLeft') {
            this.plane.x -= this.plane_speed
            return
        }
        if (code == 'ArrowRight') {
            this.plane.x += this.plane_speed
            return
        }
        if (code == 'ArrowUp') {
            this.plane.y -= this.plane_speed
            return
        }
        if (code == 'ArrowDown') {
            this.plane.y += this.plane_speed
            return
        }
        if (code == 'Space') {
            this.attack()
        }

    }
    enter_frame() {
        this.zdlist.forEach(item => {
            item.y -= 6
        })
    }
    private attack() {
        let zd = new egret.Bitmap()
        zd.texture = RES.getRes('zd1_png')
        zd.width = 64
        zd.height = 64
        zd.x = this.plane.x;
        zd.y = this.plane.y;
        this.zdlist.push(zd)
        this.addChild(zd)
    }
    private attack2() {
        const bzjson = RES.getRes('plane_json')
        const bzpng = RES.getRes('plane_png')
        var mcFactory = new egret.MovieClipDataFactory(bzjson, bzpng);
        var cacheMovie: egret.MovieClip = new egret.MovieClip(mcFactory.generateMovieClipData('plane'));

        cacheMovie.x = 0;

        cacheMovie.y = 0

        this.addChild(cacheMovie)

        cacheMovie.gotoAndPlay('bz', 1);

        cacheMovie.addEventListener(egret.Event.COMPLETE, (e: egret.Event) => {

            this.removeChild(cacheMovie)

        }, this);
    }
}